# pip install flask flask_cors sympy
from sympy import symbols, solve, latex
from sympy import * # type: ignore

def parse_expression(expression):
	'''解析表达式'''
	if isinstance(expression, dict):
		latex_solution = {k: latex(v) for k, v in expression.items()}
	elif isinstance(expression, list):
		latex_solution = [parse_expression(exp) for exp in expression]
	elif isinstance(expression, tuple):
		latex_solution = tuple(parse_expression(exp) for exp in expression)
	else:
		latex_solution = latex(expression)
	return latex_solution

def solve_equations(exp: str, vars: str)->tuple:
	'''解析并求解方程组'''
	global_namespace = globals().copy()
	exec(f'{vars}=symbols("{vars}")', global_namespace)
	globals().update(global_namespace)
	exps = eval(f'[{exp}]')
	vars = eval(f'[{vars}]')
	solution = solve(exps, vars)
	return solution, exps, vars
